package com.woniu.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.woniu.util.InjectResources.*;

/**
 * 图片上传的工具
 */
@Component
public class QiNiuUploadUtils {





    public  String uploadFile(byte[] uploadBytes, String filename){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);

        //...生成上传凭证，然后准备上传
//        String accessKey = "liVlvelDF9kP5YPVy4_MeP02NfYru0IbdoOJjluP";
//        String secretKey = "kKVBFudzBw_pVJ6xmdVxtuFX4e_FTYLrXSTV6hEz";

        //存储空间
//        String bucket = "wnxt-1201";

        //外链接域名

//        String url = "http://rr8qmqf19.hn-bkt.clouddn.com";

        //默认不指定key的情况下，以文件内容的hash值作为文件名 文件的名称
        String key = filename;

        // 签名（密码）
        Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
        String upToken = auth.uploadToken(BUCKET);

        try {
            //进行图片上传
            //参数1: 上传图片的内容 (流、byte)
            //参数2： 图片的名称
            //参数3： 图片上传的凭证 (签名)
            Response response = uploadManager.put(uploadBytes, key, upToken);

            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);

            //把每次上传的图片进行redis中记录
            //opsForSet操作set中的数据

            //返回成功的图片的url
            return URL +"/" +filename;

        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return "";
    }

    /**
     * 删除文件
     * @param fileName
     */
    public static void deleteFile(String fileName){
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        //...其他参数参考类注释
//        String accessKey = "liVlvelDF9kP5YPVy4_MeP02NfYru0IbdoOJjluP";
//        String secretKey = "kKVBFudzBw_pVJ6xmdVxtuFX4e_FTYLrXSTV6hEz";

        //存储空间
//        String bucket = "wnxt-1201";

        Auth auth = Auth.create(ACCESSKEY, SECRETKEY);
        BucketManager bucketManager = new BucketManager(auth, cfg);
        try {
            bucketManager.delete(BUCKET, fileName);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
    }
}
